package textfield;


import java.util.Vector;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.io.Reader;


/**
 * Reconstruct.java
 *
 *
 * Created: Sat Jul 31 11:20:26 2010
 *
 * @author <a href="mailto:fred@theseibels.com">Fred Seibel</a>
 * @version 1.0
 */
public class Reconstruct {
    Vector words = new Vector ();
    // Vector linesLang1 = new Vector ();
    // Vector linesLang2 = new Vector ();
    Vector codedLines1 = new Vector();
    Vector codedLines2 = new Vector();
    static int lang = 1;
    Reconstruct() throws Exception {
	Reader isr = new InputStreamReader(getClass().getResourceAsStream ("/sorted_words.txt"));
	String line = "";
	int c;
	while ((c = isr.read()) != -1) {
	    if (c == '\n') {
		words.addElement(line);
		line = "";
	    }
	    else {
		line += (char) c;
	    }
	}
	isr.close();
	int nWords = 2* words.size();
	InputStream fis = getClass().getResourceAsStream ("/sorted_lines.bin");
	int n;
	int b = 0;
	line = "";
	boolean xlatePresent = false;
	Vector nums = new Vector();
	int lineNumber = -1;
	while ((b = fis.read()) != -1) {
	    n = (b & 0xff) ;
	    b =  ((fis.read() & 0xff) << 8);
	    n = n | b;
	    if (n == nWords + 1) {
		//linesLang1.addElement(line);	
		//there is a translation
		int nArray [] = new int[nums.size()];
		for (int i = 0; i < nums.size(); i++) {
		    nArray[i] = ((Integer) nums.elementAt(i)).intValue();
		}
		nums.removeAllElements();
		codedLines1.addElement(nArray);
		xlatePresent = true;

		line = ""; //clear the english
		continue;
	    }

	    if (n == nWords) {
		lineNumber++;

		if (xlatePresent) {

		    //linesLang2.addElement(line);
		    int nArray [] = new int[nums.size()];
		    for (int i = 0; i < nums.size(); i++) {
			nArray[i] = ((Integer) nums.elementAt(i)).intValue();
		    }
		    codedLines2.addElement(nArray);

		    nums.removeAllElements();
		    line = "";

		}
		else {
		    //linesLang1.addElement(line);
		    // add English because 2nd is missing
		    // linesLang2.addElement(line);
		    int nArray [] = new int[nums.size()];
		    for (int i = 0; i < nums.size(); i++) {
			nArray[i] = ((Integer) nums.elementAt(i)).intValue();
		    }
		    nums = new Vector();
		    codedLines2.addElement(nArray);
		    codedLines1.addElement(nArray);
		}
		xlatePresent = false;
		line = "";
		continue;
	    }
	    nums.addElement(new Integer(n));
	    line += words.elementAt(n) + " ";
	}

    }
    String getLine(int n) {
	String retval = "";
	int [] nums;
	if (lang == 1) {
	    nums = (int []) (codedLines1.elementAt(n));
	}
	else {
	    nums =  (int []) (codedLines2.elementAt(n));
	}
	return decode(nums);

	// return (String) linesLang1.elementAt(n);
	// }
	// else {
	//     return (String) linesLang2.elementAt(n);
	// }

    }
    String decode(int [] nums) {
	String retval = "";
 	for (int i = 0; i < nums.length; i++) {
	    retval += words.elementAt(nums[i]);
	    if (i < nums.length  -1) {
		retval += " ";
	    }
	}
	return retval; 
    }
} // Reconstruct
